﻿#region << 文件说明 >>

/*----------------------------------------------------------------
 * 命名空间：Vampirewal.BlogCore.Controls.Container
 * 唯一标识：1e08a8fd-4d32-4a91-9696-6d307ec2e451
 * 文件名：Tabs
 *
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2024/7/26 16:25:41
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

#endregion << 文件说明 >>

namespace Vampirewal.BlogCore;

/// <summary>
/// Tabs标签页控件
/// <para>分隔内容上有关联但属于不同类别的数据集合。</para>
/// </summary>
public sealed class Tabs : ContainerControlBase<TabPane>
{
    /// <summary>
    /// 构造函数
    /// </summary>
    internal Tabs()
    {
        Children = new List<TabPane>();
    }

    public override List<TabPane> Children { get; internal set; }
    public override string Id { get; set; }
    public override string Title { get; set; }

    public override string ConverToHtml()
    {
        Param par = new Param();

        if (TabType != TabTypeEnum.Default) par.Add($"type=\"{TabType.GetDescription()}\"");

        par.Add($":tab-position=\"{TabPosition.GetDescription()}\"");

        ControlBuilder.AppendLine($"<el-tabs {par} v-model=\"{Children.First().Id}\">");

        foreach (var item in Children)
        {
            ControlBuilder.AppendLine(item.ConverToHtml());
        }

        ControlBuilder.AppendLine($"</el-tabs>");

        return base.ConverToHtml();
    }

    /// <summary>
    /// 标签类型枚举
    /// </summary>
    internal TabTypeEnum TabType { get; set; }

    /// <summary>
    /// 标签页位置
    /// </summary>
    internal TabPositionEnum TabPosition { get; set; }
}

/// <summary>
/// 标签页面板
/// </summary>
public sealed class TabPane : ControlBase
{
    internal TabPane()
    {
    }

    public override string Id { get; set; }
    public override string Title { get; set; }

    public override string ConverToHtml()
    {
        ControlBuilder.AppendLine($"<el-tab-pane label=\"{Title}\" name=\"{Id}\">");
        ControlBuilder.AppendLine(Content.ConverToHtml());
        ControlBuilder.AppendLine($"</el-tab-pane>");

        return base.ConverToHtml();
    }

    /// <summary>
    /// 内容
    /// </summary>
    internal ControlBase Content { get; set; }
}

public static partial class App
{
    /// <summary>
    /// 创建标签页
    /// </summary>
    /// <param name="action">标签页面板集合</param>
    /// <param name="type"></param>
    /// <param name="tabPosition"></param>
    /// <returns></returns>
    /// <exception cref="ArgumentNullException"></exception>
    public static Tabs CreateTabs(Action<List<TabPane>> action, TabTypeEnum type = TabTypeEnum.Default, TabPositionEnum tabPosition = TabPositionEnum.Top)
    {
        if (action == null) throw new ArgumentNullException("标签页不能为空！");

        Tabs tabs = new Tabs()
        {
            TabType = type,
            TabPosition = tabPosition
        };

        action?.Invoke(tabs.Children);

        return tabs;
    }

    /// <summary>
    /// 创建标签页面板
    /// </summary>
    /// <param name="label">标签标题</param>
    /// <param name="content">面板内容</param>
    /// <returns></returns>
    /// <exception cref="ArgumentNullException"></exception>
    public static TabPane CreateTabPane(string label, Func<ControlBase> content)
    {
        if (content == null) throw new ArgumentNullException("面板内容不能为空！");

        var result = content.Invoke();

        if (result == null) throw new ArgumentNullException("面板内容不能为空！");

        TabPane tabPane = new TabPane()
        {
            Id = GenerateId(),
            Title = label,
            Content = result
        };

        return tabPane;
    }
}

/// <summary>
/// 标签类型枚举
/// </summary>
public enum TabTypeEnum
{
    /// <summary>
    /// 默认
    /// </summary>
    [Description("")]
    Default,

    /// <summary>
    /// 卡片样式
    /// </summary>
    [Description("card")]
    Card,

    /// <summary>
    /// 默认
    /// </summary>
    [Description("border-card")]
    BorderCard
}

/// <summary>
/// 标签页位置
/// </summary>
public enum TabPositionEnum
{
    /// <summary>
    /// 上
    /// </summary>
    [Description("top")]
    Top,

    /// <summary>
    /// 右
    /// </summary>
    [Description("right")]
    Right,

    /// <summary>
    /// 下
    /// </summary>
    [Description("bottom")]
    Bottom,

    /// <summary>
    /// 左
    /// </summary>
    [Description("left")]
    Left,
}